package top.mschen.blog.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;


/**
 * WebMvcConfigurationSupport
 * WebMvcConfigurerAdapter
 * WebMvcConfigurer
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LogInterceptor logInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor).addPathPatterns("/**");
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        resolver.setViewNames("jsp/*");
        resolver.setOrder(2);
        return resolver;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");

        templateResolver.setCacheable(false);
        return templateResolver;
    }


    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        // templateEngine
        return templateEngine;
    }


    @Bean
    public ThymeleafViewResolver viewResolverThymeLeaf() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("utf-8");
        viewResolver.setOrder(1);
        viewResolver.setViewNames(new String[]{"templates/*"});
        return viewResolver;
    }

//    /**
//     * 解决乱码问题
//     * For UTF-8
//     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        WebMvcConfigurer.super.configureMessageConverters(converters);
//        converters.add(new StringHttpMessageConverter(Charset.forName(WebUtils.UTF_8)));
//    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
//
//    @Bean
//    public WebMvcConfigurer webMvcConfigurer(){
//
//        return new WebMvcConfigurer(){
//            /**
//             * 配置拦截器
//             */
//            @Override
//            public void addInterceptors(InterceptorRegistry registry) {
//                registry.addInterceptor(logInterceptor).addPathPatterns("/**");
//            }
//
//            /**
//             * 配置静态映射
//             * @param registry
//             */
////            @Override
////            public void addResourceHandlers(ResourceHandlerRegistry registry) {
////                registry.addResourceHandler("/yuqiyu/9/resources/**")
////                        .addResourceLocations("classpath:/static/");
////            }
//
//            /**
//             * 配置jsp 解析
//             * @param registry
//             */
//            @Override
//            public void configureViewResolvers(ViewResolverRegistry registry){
//                InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//                resolver.setPrefix("/WEB-INF/");
//                resolver.setSuffix(".jsp");
//                resolver.setViewNames("jsp/*");//当控制器返回的viewName符合规则时才使用这个视图解析器
//                resolver.setOrder(2);//设置优先级,数值越小优先级越高
//                registry.viewResolver(resolver);
//                registry.viewResolver( viewResolverThymeLeaf());
//           }
//        };
//    }
//
////    /**
////     * 配置静态路径映射
////     * @param registry
////     */
////    public void addResourceHandlers(ResourceHandlerRegistry registry) {
////        registry.addResourceHandler("/yuqiyu/9/resources/**")
////                .addResourceLocations("classpath:/static/");
////    }
//
////    /**
////     * addPathPatterns 添加拦截规则
////     * excludePathPatterns 排除拦截规则
////     *
////     * @param registry
////     */
////    @Override
////    public void addInterceptors(InterceptorRegistry registry) {
////        registry.addInterceptor(logInterceptor).addPathPatterns("/**");
////        super.addInterceptors(registry);
////    }
//
//    @Bean
//    public ITemplateResolver templateResolver() {
//        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
////        templateResolver.setTemplateMode("HTML");
//        templateResolver.setPrefix("classpath:/templates/");
//        templateResolver.setSuffix(".html");
//        templateResolver.setCharacterEncoding("utf-8");
//
//        templateResolver.setCacheable(false);
//        return templateResolver;
//    }
//
//
//    @Bean
//    public SpringTemplateEngine templateEngine() {
//        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver());
//        // templateEngine
//        return templateEngine;
//    }
//
//
//    @Bean
//    public ThymeleafViewResolver viewResolverThymeLeaf() {
//        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
//        viewResolver.setTemplateEngine(templateEngine());
//        viewResolver.setCharacterEncoding("utf-8");
//        viewResolver.setOrder(1);
//        viewResolver.setViewNames(new String[]{"templates/*"});
//        return viewResolver;
//    }
}
